/// 和商品相关的业务
part of pda.service;

class Product {
  static const tableName = 'pda_product';
  String id;
  String code;
  String name;
  String spec; // 规格
  String sort; // 分类
  String package; // column: package
  String packageType; // column: package_type
  String unit; // 单位
  double price; // 零售价
  double memPrice; // 会员价
  double memPrice1; // 会员价1
  double memPrice2; // 会员价2
  String providerId; // column: provider
  String producere; // 产地
  int keepDay; // 保质期
  String nature; // 单品/联营/...
  String spell; // 拼音码
  String groupMarket; // 柜组 column: group_market
  String barcode;

  Product(
      {this.id,
      this.code,
      this.name,
      this.spec,
      this.sort,
      this.package,
      this.packageType,
      this.unit,
      this.price,
      this.memPrice,
      this.memPrice1,
      this.memPrice2,
      this.providerId,
      this.producere,
      this.keepDay,
      this.nature,
      this.spell,
      this.groupMarket,
      this.barcode});

  factory Product.fromJson(Map<String, dynamic> map) {
    return Product(
      id: map['id'],
      code: map['code'],
      name: map['name'],
      spec: map['spec'],
      sort: map['sort'],
      package: map['package_'],
      packageType: map['packageType'],
      unit: map['unit'],
      price: map['price'],
      memPrice: map['memPrice'],
      memPrice1: map['memPrice1'],
      memPrice2: map['memPrice2'],
      providerId: map['providerId'],
      producere: map['producere'],
      keepDay: map['keepDay'],
      nature: map['nature'],
      spell: map['spell'],
      groupMarket: map['groupMarket'],
      barcode: map['barcode'],
    );
  }

  static String insertSql() {
    return '''insert into pda_product (
      id,
      code,
      name,
      spec,
      sort,
      package,
      package_type,
      unit,
      price,
      mem_price,
      mem_price1 ,
      mem_price2 ,
      provider_id,
      producer,
      keep_day,
      nature ,
      spell,
      group_market,
      barcode 
    ) values (
      ?,
      ?,
      ?,
      ?,
      ?,
      ?,
      ?,
      ?,
      ?,
      ?,
      ?,
      ?,
      ?,
      ?,
      ?,
      ?,
      ?,
      ?,
      ?
    )
    ''';
  }

  List<dynamic> insertSqlArgs() {
    return [
      id,
      code,
      name,
      spec,
      sort,
      package,
      packageType,
      unit,
      price,
      memPrice,
      memPrice1,
      memPrice2,
      providerId,
      producere,
      keepDay,
      nature,
      spell,
      groupMarket,
      barcode
    ];
  }

  static Product fromSqlMap(Map<String, dynamic> map) {
    return Product(
      id: map['id'],
      code: map['code'],
      name: map['name'],
      spec: map['spec'],
      sort: map['sort'],
      package: map['package'],
      packageType: map['package_type'],
      unit: map['unit'],
      price: map['price'],
      memPrice: map['mem_price'],
      memPrice1: map['mem_price1'],
      memPrice2: map['mem_price2'],
      providerId: map['provider_id'],
      producere: map['producere'],
      keepDay: map['keep_day'],
      nature: map['nature'],
      spell: map['spell'],
      groupMarket: map['group_market'],
      barcode: map['barcode'],
    );
  }

  static Future<List> query(String searchBy, String key) async {
    return await SqliteCRUD.query(
        tableName, fromSqlMap, '$searchBy like ?', [key]);
  }
}
